package cn.itcast.bos.controller;

import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.domain.common.ResponseResult;
import cn.itcast.bos.service.StandardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.*;

/**
 * Created by Administrator on 2018/12/24.
 */
@RestController
@RequestMapping("/standard")
public class StandardController {

    @Autowired
    StandardService standardService;

    @RequestMapping("/save")
    public ResponseResult save(Standard standard){

        try {
            standardService.save(standard);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.FAIL();
        }

        return ResponseResult.SUCCESS();
    }

    @RequestMapping("/pageQuery")
    public Map pageQuery(int page, int rows){

        // 调用业务层 ，查询数据结果
        Pageable pageable = PageRequest.of(page-1,rows);
        Page<Standard> pageData = standardService.findPageData(pageable);

        // 返回客户端数据 需要 total 和 rows
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("total", pageData.getTotalElements());
        result.put("rows", pageData.getContent());
        return result;
    }

    // 查询所有收派标准方法
    @RequestMapping("/findAll")
    public List<Standard> findAll() {
        List<Standard> standards = standardService.findAll();
        return standards;
    }
}
